import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_web_socket/shelf_web_socket.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

quickStart() {
  var handler = webSocketHandler((WebSocketChannel webSocket) {
    webSocket.stream.listen((message) {
      webSocket.sink.add(message);
    }).onDone(() {
      print('done ${webSocket.closeReason} ${webSocket.closeCode}');
    });
  });
  shelf_io.serve(handler, 'localhost', 8080).then((server) {
    print('Serving at ws://${server.address.host}:${server.port}');
  });
}

testClient() async {
  final channel = IOWebSocketChannel.connect(Uri.parse('wss://10.0.0.10:19999/wss/bridging'));

  channel.stream.listen((message) {
    print('Received: $message');
  });

  channel.sink.add('Hello server!');

  // 关闭WebSocket连接
  await channel.sink.close();
}

testServer() async {
  final handler = webSocketHandler((WebSocketChannel webSocket) {
    webSocket.stream.listen((message) {
      print('Received: $message');
      webSocket.sink.add('Hello client!');
    });
  });

  final server = await shelf_io.serve(handler, '127.0.0.1', 8082);
  print('Server running on localhost:8081');

  // 关闭服务器
  // server.then((server) => server.close());
}


void main() {
  testServer();
  // sleep(Duration(seconds: 600));
  // testClient();
  // quickStart();
}
